<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    window.onload = function () {
        setInterval("showTime()",1000)
    }

    function showTime() {
        let date = new Date();
        // 获取年月日
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();

        // 获取时分秒
        let hours = date.getHours();
        let minutes = date.getMinutes();
        let seconds = date.getSeconds();

        // 获取上下午
        let a = "AM";
        if (hours > 12) {
            a = "PM";
            hours -= 12;
        }

        // 获取星期数
        let weekdaynum = date.getDay();
        let weekday;
        console.log(weekdaynum)

        switch (weekdaynum) {
            case 0:
                weekday = "星期日";
                break;
            case 1:
                weekday = "星期一";
                break;
            case 2:
                weekday = "星期二";
                break;
            case 3:
                weekday = "星期三";
                break;
            case 4:
                weekday = "星期四";
                break;
            case 5:
                weekday = "星期五";
                break;
            case 6:
                weekday = "星期六";
                break;
        }

        let str = year + "年" + month + "月" + day + "日" + " " + hours + ":" + minutes + ":" + seconds + " " + a + " " + weekday;
        document.getElementById("datetime").innerText = str;
    }

</script>
<div>
    <span>你好，现在是北京时间：</span><br>
    <span id="datetime">

    </span>
</div>

</body>
</html>
